﻿using System;
using nkstdgia.main.report;
using wsRK.Web;
using System.Web.Security;
using wsRK.AppUser;
using System.Web.UI;
using nkstdgia.main.usercontrol;

namespace nkstdgia.main
{
    public partial class mainpage : WebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Control ctrl;
            MembershipUser loginUser = MembershipDataservice.getUser(User.Identity.Name);
            if (loginUser == null)
            {
                this.bang_khao_sat.Visible = false;
                //ctrl = this.LoadControl("usercontrol/uc_MainPage_Link4Guest.ascx");
                //this.phoLink.Controls.Add(ctrl);
            }
            else if (User.IsInRole("Sinh viên"))
            {
                ctrl = this.LoadControl("usercontrol/uc_MainPage_Link4Student.ascx");
                this.phoLink.Controls.Add(ctrl);
            }
            else if (User.IsInRole("Giảng viên"))
            {
                ctrl = this.LoadControl("usercontrol/uc_MainPage_Link4Teacher.ascx");
                (ctrl as uc_MainPage_Link4Teacher).ReportDelegateClick += new uc_MainPage_Link4Teacher.ReportDelegate(Export_Click);
                this.phoLink.Controls.Add(ctrl);
            }
            else if (User.IsInRole("Quản trị viên"))
            {
                ctrl = this.LoadControl("usercontrol/uc_MainPage_Link4Admin.ascx");
                (ctrl as uc_MainPage_Link4Admin).ReportDelegateClick += new uc_MainPage_Link4Admin.ReportDelegate(Export_Click);
                this.phoLink.Controls.Add(ctrl);
            }
            else //administrator
            {
                ctrl = this.LoadControl("usercontrol/uc_MainPage_Link4Admin.ascx");
                (ctrl as uc_MainPage_Link4Admin).ReportDelegateClick += new uc_MainPage_Link4Admin.ReportDelegate(Export_Click);
                this.phoLink.Controls.Add(ctrl);
            }
        }

        protected void Export_Click()
        {
            try
            {
                ExcelCommand eCommand = new ExcelCommand();
                string filename = eCommand.exportGiaDoanDuong();
                base.hideLoader();
                if (!string.IsNullOrEmpty(filename))
                {
                    Response.Redirect("/FileDownload.ashx?type=" + filename);
                }
            }
            catch (Exception ex)
            {
                base.showMasterMessage("Có lỗi trong quá trình tính dữ liệu !");
            }
            finally
            {
                //base.hideLoader();
            }
        }
    }
}
